//
//  SettingViewController.m
//  Pay
//
//  Created by Jayce on 15/1/20.
//  Copyright (c) 2015年 steve. All rights reserved.
//


#import "SettingViewController.h"
#import "commonDefines.h"
#import <QuartzCore/QuartzCore.h>

@interface SettingViewController ()

@property (weak, nonatomic) IBOutlet UITextField *eachText;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _cancel_btn.layer.borderColor = HexRGB(0x676767).CGColor;
    _cancel_btn.layer.borderWidth = 1.0;
    _save_btn.layer.borderWidth = 1.0;
    _save_btn.layer.borderColor = HexRGB(0x74CF1C).CGColor;
    //添加点击背景 取消键盘手势；
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backgroundTab)];
    [self.view addGestureRecognizer:tap];
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    //start: fill in self.eachText.text as GREEN_SETTING_EACH.  added by william 20150211
    if ([[NSUserDefaults standardUserDefaults] objectForKey:GREEN_SETTING_EACH] != nil ) {
        self.eachText.text = [[NSUserDefaults standardUserDefaults] objectForKey:GREEN_SETTING_EACH];
    }
    //end
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    [self replaceTopConstraintOnView:self.setting_bgd_view withConstant:109];
    [self animateConstraints];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    [self replaceTopConstraintOnView:self.setting_bgd_view withConstant:209];
    [self animateConstraints];
}
- (void)backgroundTab
{
    [self.eachText resignFirstResponder];
}
#pragma mark - Helper Methods

- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
    [self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
        if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
            constraint.constant = constant;
        }
    }];
}

- (void)animateConstraints
{
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

#pragma mark - private -

- (void)saveEach:(NSString *)each
{
    NSAssert(each, @"each 不可空");
    [[NSUserDefaults standardUserDefaults] setObject:each
                                              forKey:GREEN_SETTING_EACH];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - action -

- (IBAction)onCancel:(id)sender {
    [_eachText resignFirstResponder];
    [self.view removeFromSuperview];
}

- (IBAction)onSave:(id)sender {
    if ([self.eachText.text length] == 0) {
        [[[UIAlertView alloc] initWithTitle:nil
                                    message:@"Must have numbers."
                                   delegate:nil
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil] show];
        return;
    }
    
    [self saveEach:self.eachText.text];
    
    [self.view removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
